<template>
  <el-container class="out-el-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="@/assets/heima.png" alt="" />
        <span>决裁者后台管理系统</span>
      </div>
      <el-button type="info" @click="backHome">退出</el-button>
    </el-header>
    <el-container style="height: 522.77px">
      <!-- 侧边栏区域 -->
      <el-aside :width="isfold ? '64px' : '200px'">
        <el-tooltip class="item" effect="dark" :content="isfold ? '打开' : '折叠'" placement="right-start" :enterable="false">
          <div class="aside_fold" @click="isAsideFold">|||</div>
        </el-tooltip>
        <!-- router属性(默认值是false)开启后,是使用 vue-router 的模式，启用该模式会在激活导航时(即点击二级导航时)以 index 作为 path 进行路由跳转 -->
        <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff" unique-opened :collapse="isfold" :collapse-transition="false" router :default-active="ActivatPath">
          <!-- 一级导航 -->
          <!-- elmentui库 在这的 index值必须为字符串类型 -->
          <el-submenu :index="todo.id + ''" v-for="todo in menuList" :key="todo.id">
            <!-- 一级导航模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[todo.id]"></i>
              <!-- 文本 -->
              <span>{{ todo.authName }}</span>
            </template>
            <!-- 2级导航 -->
            <!-- index值作为路由的path 进行跳转 可以写'/' + menuList_children.path或直接 menuList_children.path, elmentui会帮加 -->
            <el-menu-item :index="menuList_children.path" v-for="menuList_children in todo.children" :key="menuList_children.id" @click="currentaActivatIndex(menuList_children.path)">
              <!-- 2级导航模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ menuList_children.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
import commonAPI from '@/api/commonAPI/commonAPI.js'
export default {
  data() {
    return {
      // 控制菜单是否折叠
      isfold: false,
      // 左侧菜单数据
      menuList: [],
      iconList: {
        // 左侧菜单图标数据
        125: 'iconfont icon-icon_user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      // 当前被激活的链接
      ActivatPath: ''
    }
  },
  created() {
    this.getMenu()
    this.ActivatPath = window.sessionStorage.getItem('ActivatPath')
  },
  methods: {
    backHome() {
      // 基于token值退出home页面,只要把sessionStroage中的token值移除即可,
      // 后续的请求不会携带token值,必须重新登录生成一个新的token值能访问页面.
      window.sessionStorage.removeItem('token')
      // 编程式导航强制挑转到登录页
      this.$router.push('/login')
    },
    async getMenu() {
      // 请求菜单数据
      const { data: res } = await commonAPI({ method: 'get', url: 'menus' })
      this.menuList = res.data
    },
    isAsideFold() {
      this.isfold = !this.isfold
    },
    currentaActivatIndex(ActivatPath) {
      // 存储下当前被激活的链接
      window.sessionStorage.setItem('ActivatPath', ActivatPath)
      this.ActivatPath = window.sessionStorage.getItem('ActivatPath')
    }
  }
}
</script>

<style lang="less" scoped>
.out-el-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-left: 0;
    & > div {
      display: flex;
      align-items: center;
      & > span {
        margin-left: 10px;
        font-size: 20px;
        color: #fff;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .el-menu {
      border-right: none;
    }
    .aside_fold {
      background-color: rgba(204, 204, 204, 0.116);
      text-align: center;
      line-height: 24px;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
      letter-spacing: 0.2px;
      cursor: pointer;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
.iconfont {
  margin-right: 8px;
}
</style>
